import java.util.Stack;

public class ValidateStackSequences {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed.length==0||pushed.length!=popped.length){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int j =0;
        for (int i=0;i<pushed.length;i++){
            stack.push(pushed[i]);
            if (!stack.isEmpty()&&stack.peek()==popped[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();

    }
}
